package com.dusty.boring.mybatis.sql.autoconfig;

import com.dusty.boring.mybatis.sql.common.utils.JsonUtils;
import com.dusty.boring.mybatis.sql.intercept.BadSqlValidateIntercepter;
import com.dusty.boring.mybatis.sql.validater.provider.MySqlValidateProvider;
import com.dusty.boring.mybatis.sql.validater.provider.OracleValidateProvider;
import jdk.nashorn.internal.ir.debug.JSONWriter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jackson.JsonObjectDeserializer;
import org.springframework.boot.json.JsonParser;
import org.springframework.boot.json.JsonSimpleJsonParser;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.List;

/**
 * <pre>
 *
 *       <Sql检查自动配置>
 *
 * @author xiehongfei[xie_hf@suixingpay.com]
 * @date: 2019年04月13日 21:58
 * @version V1.0
 * @review: xiehongfei[xie_hf@suixingpay.com]/2019年04月13日 21:58
 * </pre>
 */
@Slf4j
@ConditionalOnBean(DataSource.class)
@ConditionalOnClass({ SqlSessionFactory.class})
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
@EnableConfigurationProperties(SqlValidatorProperties.class)
@RequiredArgsConstructor(onConstructor =  @__(@Autowired))
public class SqlValidatorAutoConfigure {

    private final List<SqlSessionFactory> sqlSessionFactories;
    private final SqlValidatorProperties sqlValidatorProperties;
    
    @PostConstruct
    public void autoConfigCheck() {
        
        log.info("\n-\t 当前拦截器参数:{}", JsonUtils.object2String(sqlValidatorProperties.toString()));
        
        //~~ 注入插件 ~~
        sqlSessionFactories.forEach(sqlSessionFactory -> sqlSessionFactory.getConfiguration().addInterceptor(new BadSqlValidateIntercepter()));
    }
    
    /**
     * <pre>
     *     注入SQL拦截器
     *
     * @return  拦截器实例
     * </pre>
     */
//    @Bean
//    public BadSqlValidateIntercepter injectBadSqlValidateIntercepterHandler() {
//        return new BadSqlValidateIntercepter();
//    }
    
    /**
     * <pre>
     *     注入MySqlValidateProvider
     *
     * @return mySqlValidateProvider
     * </pre>
     */
    @Bean
    public MySqlValidateProvider mySqlValidateProvider() {
        return new MySqlValidateProvider(sqlValidatorProperties);
    }
    
    /**
     * <pre>
     *     注入OracleValidateProvider
     *
     * @return oracleValidateProvider
     * </pre>
     */
    @Bean
    public OracleValidateProvider oracleValidateProvider() {
        return new OracleValidateProvider(sqlValidatorProperties);
    }

}
